home *** CD-ROM | disk | FTP | other *** search
/ Resource for Source: C/C++ / Resource for Source - C-C++.iso / codelib7 / v_09_06 / phillips.exe / DISPLAY.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-09-08  |  14.8 KB  |  539 lines

  1.  
  2.        /*****************************************************
  3.        *
  4.        *       file d:\cips\display.c
  5.        *
  6.        *       Functions: This file contains
  7.        *          display_image
  8.        *          display_image_portion
  9.        *          display_menu_for_display_image
  10.        *          map_16_shades_of_gray
  11.        *          transform_the_colors
  12.        *
  13.        *       Purpose:
  14.        *          These functions display images on a the
  15.        *          monitor.
  16.        *
  17.        *       External Calls:
  18.        *          rtiff.c - read_tiff_image
  19.        *          cips.c - clear_text_screen
  20.        *          hist.c - zero_histogram
  21.        *                   calculate_histogram
  22.        *                   perform_histogram_equalization
  23.        *
  24.        *       Modifications:
  25.        *          17 June 1987 - created
  26.        *          August 1990 - extension modifications for use
  27.        *              in the C Image Processing System.
  28.        *
  29.        ********************************************************/
  30.  
  31.  
  32. #include "d:\cips\cips.h"
  33.  
  34.  
  35.  
  36.  
  37.  
  38.    /***************************
  39.    *
  40.    *   display_image(...
  41.    *
  42.    ****************************/
  43.  
  44.  
  45. display_image(file_name, image, il, ie, ll, le,
  46.               image_header, monitor_type, color_transform,
  47.               invert, image_colors, display_colors,
  48.               show_hist)
  49.    char    color_transform[],
  50.            file_name[],
  51.            monitor_type[];
  52.    int     display_colors,
  53.            image_colors,
  54.            invert,
  55.            il,
  56.            ie,
  57.            ll,
  58.            le,
  59.            show_hist;
  60.    short   image[ROWS][COLS];
  61.    struct  tiff_header_struct *image_header;
  62. {
  63.    char  channels[80],
  64.          response[80];
  65.  
  66.    int   a,
  67.          b,
  68.          c,
  69.          channel,
  70.          count,
  71.          display_mode,
  72.          key,
  73.          horizontal,
  74.          max_horizontal,
  75.          max_vertical,
  76.          not_finished,
  77.          r,
  78.          vertical,
  79.          x_offset,
  80.          y_offset;
  81.  
  82.    unsigned int block,
  83.                 color,
  84.                 i,
  85.                 j,
  86.                 x,
  87.                 y;
  88.  
  89.    unsigned long histogram[256], new_hist[256];
  90.  
  91.  
  92.  
  93.      /*************************************************
  94.      *
  95.      *   If you want to display the histogram and do not
  96.      *   want to perform hist equalization, then
  97.      *   zero the histogram for calculations.
  98.      *
  99.      *************************************************/
  100.  
  101.    if(  (show_hist == 1)   &&
  102.         (color_transform[0] != 'H'))
  103.       zero_histogram(histogram);
  104.  
  105.    not_finished = 1;
  106.    while(not_finished){
  107.  
  108.  
  109.       if(display_colors == 16){
  110.          if(monitor_type[0] == 'V'){
  111.             horizontal   = 4;
  112.             vertical     = 6;
  113.             display_mode = _VRES16COLOR; /* MSC 6.0 */
  114.          }  /* ends if V */
  115.          if(monitor_type[0] == 'E'){
  116.             horizontal   = 3;
  117.             vertical     = 6;
  118.             display_mode = _ERESCOLOR; /* MSC 6.0 */
  119.          }  /* ends if E */
  120.  
  121.       }  /* ends if colors == 16 */
  122.  
  123.       else{
  124.          horizontal   = 2;
  125.          vertical     = 2;
  126.          display_mode = _MAXCOLORMODE; /* MSC 6.0 */
  127.       }
  128.  
  129.  
  130.       max_horizontal = (image_header->image_length+50)/100;
  131.       max_vertical   = (image_header->image_width+50)/100;
  132.  
  133.       if(horizontal > max_horizontal) horizontal = max_horizontal;
  134.       if(vertical > max_vertical) vertical = max_vertical;
  135.  
  136.  
  137.  
  138.         /****************************************
  139.         *
  140.         *   If color transform wants histogram
  141.         *   equalization, then read in the
  142.         *   image arrays and calculate the
  143.         *   histogram.   Zero both the histogram
  144.         *   and the new_hist.  You will need the
  145.         *   new_hist if you want to display the
  146.         *   equalized hist.
  147.         *
  148.         *****************************************/
  149.  
  150.       if(color_transform[0] == 'H'){
  151.          count = 1;
  152.          zero_histogram(histogram);
  153.          zero_histogram(new_hist);
  154.          for(a=0; a<vertical; a++){
  155.             for(b=0; b<horizontal; b++){
  156.  
  157.                x = a*100;
  158.                y = b*100;
  159.  
  160.                printf("\nDISPLAY> Calculating histogram");
  161.                printf(" %d of %d",count,vertical*horizontal);
  162.                count++;
  163.                read_tiff_image(file_name, image, il+y,
  164.                             ie+x, ll+y, le+x);
  165.                calculate_histogram(image, histogram);
  166.  
  167.             }  /* ends loop over b */
  168.          }  /* ends loop over a */
  169.       }  /* ends if display_mode == H */
  170.  
  171.  
  172.  
  173.         /* set graphics mode */
  174.  
  175.    _setvideomode(display_mode); /* MSC 6.0 */
  176.    if(display_colors == 16) map_16_shades_of_gray(display_mode);
  177.  
  178.  
  179.  
  180.         /****************************************
  181.         *
  182.         *   Loop over this size and
  183.         *   read and display ROWSxCOLS arrays.
  184.         *
  185.         *   If you want to show the histogram AND
  186.         *   do not want to do hist equalization
  187.         *   then calculate the hist from the 
  188.         *   original image array.
  189.         *
  190.         *   If you want to do hist equalization
  191.         *   then calculate the new_hist AFTER
  192.         *   the image has been equalized by the
  193.         *   the transform_the_colors function.
  194.         *
  195.         *   NOTE: Remember that the function
  196.         *   transform_the_colors changes the
  197.         *   gray shade values in image array.
  198.         *
  199.         *****************************************/
  200.  
  201.       for(a=0; a<vertical; a++){
  202.          for(b=0; b<horizontal; b++){
  203.  
  204.             x = a*100;
  205.             y = b*100;
  206.             read_tiff_image(file_name, image, il+y, 
  207.                             ie+x, ll+y, le+x);
  208.             if(  (show_hist == 1)  &&
  209.                  (color_transform[0] != 'H'))
  210.                calculate_histogram(image, histogram);
  211.  
  212.             transform_the_colors(image, color_transform,
  213.                                  display_colors,
  214.                                  image_colors, histogram,
  215.                                  horizontal, vertical);
  216.  
  217.             if(color_transform[0] == 'H')
  218.                calculate_histogram(image, new_hist);
  219.             display_image_portion(image, x, y, display_colors, 
  220.                                   image_colors, invert);
  221.          }        /* ends loop over b */
  222.       }        /* ends loop over a */
  223.  
  224.  
  225.          /***************************
  226.          *
  227.          *   if show_hist == 1 then
  228.          *   display the histogram
  229.          *   in the lower right hand
  230.          *   corner of screen
  231.          *
  232.          *   If hist equalization was
  233.          *   performed then show the
  234.          *   new_hist.  If it wasn't
  235.          *   done then show the original
  236.          *   histogram.
  237.          *
  238.          ****************************/
  239.  
  240.       if(show_hist == 1){
  241.          if(monitor_type[0] == 'V')
  242.             y_offset = 470;
  243.          if(monitor_type[0] == 'E')
  244.             y_offset = 310;
  245.          x_offset = 380;
  246.          if(color_transform[0] == 'H')
  247.             display_histogram(new_hist, x_offset,
  248.                    y_offset, 10, 15);
  249.          else
  250.             display_histogram(histogram, x_offset,
  251.                    y_offset, 10, 15);
  252.       }
  253.  
  254.       read_string(response);
  255.       printf("\nEnter 0 to quit 1 to do again");
  256.       get_integer(¬_finished);
  257.  
  258.           /* set display back to text mode */
  259.       clear_text_screen();
  260.  
  261.  
  262.    }  /* ends while not_finished  */
  263.  
  264. }  /* ends main  */
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.    /***********************************************
  273.    *
  274.    *   display_menu_for_display_image(
  275.    *
  276.    ************************************************/
  277.  
  278.  
  279. display_menu_for_display_image(image_colors, display_colors,
  280.                               invert, color_transform, 
  281.                               monitor_type, 
  282.                               show_hist)
  283.    char color_transform[], monitor_type[];
  284.    int  *invert, *image_colors, *display_colors, *show_hist;
  285. {
  286.    char response[80];
  287.    int  int_response, not_finished, r;
  288.  
  289.    not_finished = 1;
  290.    while(not_finished){
  291.       printf("\n\nDISPLAY> Enter choice (0 for no change) ");
  292.       printf("\nDISPLAY> 1. Invert is %d (1=on 0=off)", *invert);
  293.       printf("\nDISPLAY> 2. Color Transform-- %s", 
  294.              color_transform);
  295.       printf("\nDISPLAY> 3. Input image has %d colors", 
  296.              *image_colors);
  297.       printf("\nDISPLAY> 4. Display will show %d colors", 
  298.              *display_colors);
  299.       printf("\nDISPLAY> 5. Monitor type is %s",
  300.              monitor_type);
  301.       printf("\nDISPLAY> 6. Histogram is %d", *show_hist);
  302.       printf("  (1=show 0=don't show)");
  303.       printf("\nDISPLAY> _\b");
  304.       get_integer(&r);
  305.  
  306.       if(r == 0){
  307.          not_finished = 0;
  308.       }
  309.  
  310.       if(r == 1){
  311.          printf("\nDISPLAY> Enter 1 for invert on");
  312.          printf(" 0 for invert off");
  313.          printf("\nDISPLAY> ___");
  314.          get_integer(&int_response);
  315.          *invert = int_response;
  316.       }  /* ends if r == 1 */
  317.  
  318.       if(r == 2){
  319.          printf("\nDISPLAY> Enter the new color transform mode ");
  320.          printf("\nDISPLAY> (S) Straight mode");
  321.          printf("   (H) Histogram Equalization");
  322.          printf("\nDISPLAY> _\b");
  323.          read_string(response);
  324.          if((response[0] == 'S') ||
  325.             (response[0] == 's'))
  326.                strcpy(color_transform, "Straight mode");
  327.          else
  328.                strcpy(color_transform,
  329.                 "Histogram Equalization mode");
  330.       }  /* ends if r == 2  */
  331.  
  332.       if(r == 3){
  333.          printf("\nDISPLAY> Enter the number of colors");
  334.          printf(" in the input image");
  335.          printf("\nDISPLAY> ___");
  336.          get_integer(&int_response);
  337.          *image_colors = int_response;
  338.       }  /* ends if r == 3 */
  339.  
  340.       if(r == 4){
  341.          printf( 
  342.           "\nDISPLAY> Enter the number of colors for the display");
  343.          printf("\nDISPLAY> ___");
  344.          get_integer(&int_response);
  345.          *display_colors = int_response;
  346.       }  /* ends if r == 4 */
  347.  
  348.       if(r == 5){
  349.          printf("\nDISPLAY> Enter the new monitor type");
  350.          printf("\nDISPLAY> (E) EGA   (V) VGA");
  351.          printf("   (C) CGA   (M) Monochrome");
  352.          printf("\nDISPLAY> _\b");
  353.          read_string(response);
  354.          if((response[0] == 'E') ||
  355.             (response[0] == 'e'))
  356.             strcpy(monitor_type, "EGA");
  357.       if((response[0] == 'V') ||
  358.          (response[0] == 'v'))
  359.             strcpy(monitor_type, "VGA");
  360.       if((response[0] == 'C') ||
  361.          (response[0] == 'c'))
  362.             strcpy(monitor_type, "CGA");
  363.       if((response[0] == 'M') ||
  364.          (response[0] == 'm'))
  365.             strcpy(monitor_type, "Monochrome");
  366.       }  /* ends if r == 5  */
  367.  
  368.       if(r == 6){
  369.          printf(
  370.             "\nDISPLAY> Enter 1 for show histogram 0 for don't");
  371.          printf("\nDISPLAY> ___");
  372.          get_integer(&int_response);
  373.          *show_hist = int_response;
  374.       }  /* ends if r == 6 */
  375.  
  376.  
  377.  
  378.    }  /* ends while not_finished  */
  379. }  /* ends display_menu  */
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.  
  389.    /********************************
  390.    *
  391.    *   display_image_portion(...
  392.    *
  393.    *********************************/
  394.  
  395.  
  396.  
  397.  
  398. display_image_portion(image, x, y, display_colors, image_colors,
  399.                       invert)
  400.    int      invert, display_colors, image_colors;
  401.    short    image[ROWS][COLS];
  402.    unsigned int x, y;
  403. {
  404.    unsigned int color, i, j;
  405.  
  406.  
  407.       if(invert == 1){
  408.         for(i=0; i<ROWS; i++)
  409.            for(j=0; j<COLS; j++)
  410.               image[i][j] = (display_colors-1)-image[i][j];
  411.       }  /* ends if invert == 1 */
  412.  
  413.  
  414.       for(i=0; i<ROWS; i++){
  415.          for(j=0; j<COLS; j++){
  416.  
  417.             _setcolor(image[i][j]);     /* MSC 6.0 statements */
  418.             _setpixel(j+x, i+y);
  419.  
  420.          }  /* ends loop over j  */
  421.       }     /* ends loop over i  */
  422.  
  423. }  /* ends display_image_portion  */
  424.  
  425.  
  426.  
  427.  
  428.  
  429.  
  430.  
  431.  
  432.  
  433.    /**********************************************
  434.    *
  435.    *   map_16_shades_of_gray(...
  436.    *
  437.    *   This function maps 16 shades of gray into
  438.    *   the first 16 color indices.  This allows
  439.    *   you to display a true "black and white"
  440.    *   image on a color monitor.
  441.    *
  442.    *********************************************/
  443.  
  444.  
  445. map_16_shades_of_gray(display_mode)
  446.    int display_mode;
  447. {
  448.    /* all MSC 6.0 statements */
  449. _setvideomode(display_mode);
  450. _remappalette(0,  0x000000L);
  451. _remappalette(1,  0x040404L);
  452. _remappalette(2,  0x080808L);
  453. _remappalette(3,  0x0c0c0cL);
  454. _remappalette(4,  0x101010L);
  455. _remappalette(5,  0x141414L);
  456. _remappalette(6,  0x181818L);
  457. _remappalette(7,  0x1c1c1cL);
  458. _remappalette(8,  0x202020L);
  459. _remappalette(9,  0x242424L);
  460. _remappalette(10, 0x282828L);
  461. _remappalette(11, 0x2c2c2cL);
  462. _remappalette(12, 0x303030L);
  463. _remappalette(13, 0x343434L);
  464. _remappalette(14, 0x383838L);
  465. _remappalette(15, 0x3f3f3fL);
  466. }
  467.  
  468.  
  469.  
  470.  
  471.  
  472.  
  473.    /*********************************************
  474.    *
  475.    *   transform_the_colors(...
  476.    *
  477.    *   This function transforms the gray shades
  478.    *   in the image array for display.  It can either
  479.    *   do it in straight mode by multiplying or 
  480.    *   dividing or it can do it with hist 
  481.    *   equalization by calling the function
  482.    *   perform_histogram_equalization.
  483.    *
  484.    *************************************************/
  485.  
  486.  
  487. transform_the_colors(image, color_transform,
  488.                      display_colors, image_colors,
  489.                      histogram, horizontal,
  490.                      vertical)
  491.    char   color_transform[];
  492.    int    display_colors, horizontal,
  493.           image_colors, vertical;
  494.    short  image[ROWS][COLS];
  495.    unsigned long histogram[];
  496. {
  497.    int         color, i, j;
  498.    float new_grays, area;
  499.    unsigned long x;
  500.  
  501.    if(color_transform[0] == 'S'){
  502.       for(i=0; i<ROWS; i++){
  503.          for(j=0; j<COLS; j++){
  504.  
  505.  
  506.             if( (display_colors == 16) &&
  507.                 (image_colors  == 256))
  508.                color = image[i][j]/16;
  509.             if( (display_colors == 16) &&
  510.                 (image_colors  == 16))
  511.                color = image[i][j];
  512.             if( (display_colors == 256) &&
  513.                 (image_colors  == 256))
  514.                color = image[i][j];
  515.             if( (display_colors == 256) &&
  516.                 (image_colors  == 16))
  517.                color = image[i][j]*16;
  518.  
  519.             image[i][j] = color;
  520.  
  521.  
  522.          }  /* ends loop over j        */
  523.       }        /* ends loop over i        */
  524.    }  /* ends if transform is straight */
  525.  
  526.  
  527.    if(color_transform[0] == 'H'){
  528.  
  529.       area      = ((long)(vertical))*((long)(horizontal));
  530.       area      = area*10000.0;
  531.       new_grays = display_colors;
  532.  
  533.       perform_histogram_equalization(image, histogram,
  534.                                      new_grays, area);
  535.  
  536.    }  /* ends if transform is hist equalization */
  537.  
  538. }  /* ends transform_the_colors */
  539.